//
//  KeyboardModel.m
//  HAODE
//
//  Created by panquanReabam.com on 15/10/21.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "KeyboardModel.h"

#import <UIKit/UIKit.h>

//屏幕宽度
#define WIDTH_SCREEN [UIScreen mainScreen].applicationFrame.size.width
//屏幕高度
#define HEIGHT_SCREEN [UIScreen mainScreen].applicationFrame.size.height

@implementation KeyboardModelboard
@synthesize mDelegate;

static KeyboardModelboard *customKeyboard = nil;

+(KeyboardModelboard *)customKeyboard
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [[KeyboardModelboard alloc] init];
        }
        return customKeyboard;
    }
}
+(id)allocWithZone:(struct _NSZone *)zone //确保使用者alloc时 返回的对象也是实例本身
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [super allocWithZone:zone];
        }
        return customKeyboard;
    }
}
+(id)copyWithZone:(struct _NSZone *)zone //确保使用者copy时 返回的对象也是实例本身
{
    return customKeyboard;
}

-(void)textViewShowView:(UIViewController *)viewController  String1:(NSString *)String  pinglunshuString1:(NSString *)pinglunshuString  customKeyboardDelegate:(id)delegate
{
    self.mViewController =viewController;
    self.mDelegate =delegate;
    self.isTop = NO;//初始化的时候设为NO
    
    self.mBackView =[[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-50, WIDTH_SCREEN, 50)];
    NSLog(@"%p",self.mBackView);
    self.mBackView.backgroundColor =[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.9];
    self.mBackView.layer.borderWidth = 0.35;
    self.mBackView.layer.borderColor = qianhuicolor.CGColor;
    [self.mViewController.view addSubview:self.mBackView];
    
    self.mSecondaryBackView =[[UIView alloc]initWithFrame:CGRectMake(ScreenWidth*90/750, 8, ScreenWidth*560/750, 32.5)];
    self.mSecondaryBackView.backgroundColor =[UIColor clearColor];
    [self.mBackView addSubview:self.mSecondaryBackView];
    
 
    
    self.mTextView=[[UITextField alloc]initWithFrame:CGRectMake(0.5, 0, ScreenWidth*560/750-1, 32.5)];
    self.mTextView.borderStyle = UITextBorderStyleRoundedRect;
    self.mTextView.font = [UIFont systemFontOfSize: contenfot];
    self.mTextView.textColor=[UIColor blackColor];
    self.mTextView.returnKeyType=UIReturnKeyDone;
    self.mTextView.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    self.mTextView.tag=5;
    self.mTextView.placeholder = @"我来说两句..."; //默认显示的字
    [self.mTextView setUserInteractionEnabled:YES];
    self.mTextView.delegate=self;
    self.mTextView.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.mSecondaryBackView addSubview:self.mTextView];

 
    
    self.mguanzhuBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    self.mguanzhuBtn.frame =CGRectMake(15, 16, 20, 20);
    [self.mguanzhuBtn setBackgroundColor:[UIColor clearColor]];
    if ([String isEqualToString:@"Y"]) {
        [self.mguanzhuBtn setImage:[UIImage imageNamed:@"shoucang-hight"] forState:UIControlStateNormal];
    }else{
        [self.mguanzhuBtn setImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
    }
    
    [self.mguanzhuBtn addTarget:self action:@selector(mguanzhu:) forControlEvents:UIControlEventTouchUpInside];
    [self.mBackView addSubview:self.mguanzhuBtn];

    self.mTalkBtn =[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*665/750, 16, 40, 18)];
    [self.mTalkBtn setTitle:[NSString stringWithFormat:@"%@",  pinglunshuString] forState:UIControlStateNormal];
    [self.mTalkBtn addTarget:self action:@selector(forTalk) forControlEvents:UIControlEventTouchUpInside];
    [self.mTalkBtn setTitleColor:shenhuicolor forState:UIControlStateNormal];
    self.mTalkBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    [self.mBackView addSubview:self.mTalkBtn];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.mTextView resignFirstResponder];
    
    [mDelegate pinglunBtn:self.mTextView];

    return YES;
}


-(void)mguanzhu:(UIButton*)sender //关注按钮
{
    
  
    [mDelegate guangzhuBtn:sender.titleLabel.text];

    
}

-(void)forTalk //评论按钮
{
     [self.mTextView resignFirstResponder];
    [mDelegate pinglunBtnjiemian:@"jiemian"];

}
-(void)hideView //点击屏幕其他地方 键盘消失
{
    if (self.mTextView.text.length==0)
    {
     
        [self.mTextView resignFirstResponder];
    }
    else
    {
        [self.mTextView resignFirstResponder];
    }
    
    
}

- (void)keyboardWillShow:(NSNotification*)notification //键盘出现
{
    self.isTop =YES;
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSLog(@"%f-%f",_keyboardRect.origin.y,_keyboardRect.size.height);
    
    if (!self.mHiddeView)
    {
        self.mHiddeView =[[UIView alloc]initWithFrame:CGRectMake(0, 0,WIDTH_SCREEN,HEIGHT_SCREEN)];
        self.mHiddeView.backgroundColor =[UIColor blackColor];
        self.mHiddeView.alpha =0.0f;
        [self.mViewController.view addSubview:self.mHiddeView];
        
        UIButton *hideBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        hideBtn.backgroundColor =[UIColor clearColor];
        hideBtn.frame = self.mHiddeView.frame;
        [hideBtn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
        [self.mHiddeView addSubview:hideBtn];
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.mHiddeView.alpha =0.4f;
        self.mBackView.frame =CGRectMake(0, HEIGHT_SCREEN-_keyboardRect.size.height-30, WIDTH_SCREEN, 50);
        [self.mViewController.view bringSubviewToFront:self.mBackView];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)keyboardWillHide:(NSNotification*)notification //键盘下落
{
    self.isTop =NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.mBackView.frame=CGRectMake(0, HEIGHT_SCREEN-30, WIDTH_SCREEN, 50);
        self.mHiddeView.alpha =0.0f;
        self.mSecondaryBackView.frame =CGRectMake(ScreenWidth*90/750, 10, ScreenWidth*560/750, 32.5);
        
    } completion:^(BOOL finished) {
        [self.mHiddeView removeFromSuperview];
        self.mHiddeView =nil;
        //        self.mTextView.text =@"我来说两句..."; //键盘消失时，恢复TextView内容
    }];
}
//- (void)textDidChanged:(NSNotification *)notif //监听文字改变 换行时要更改输入框的位置
//{
//    CGSize contentSize = self.mTextView.contentSize;
//    
//    if (contentSize.height > 140){
//        return;
//    }
//    CGFloat minus = 3;
//    CGRect selfFrame = self.mBackView.frame;
//    CGFloat selfHeight = self.mTextView.superview.frame.origin.y * 2 + contentSize.height - minus + 2 * 2;
//    CGFloat selfOriginY = selfFrame.origin.y - (selfHeight - selfFrame.size.height);
//    selfFrame.origin.y = selfOriginY;
//    selfFrame.size.height = selfHeight;
//    self.mBackView.frame = selfFrame;
//    self.mSecondaryBackView.frame =CGRectMake(ScreenWidth*90/750, 10, ScreenWidth*560/750, selfHeight-20);
//    NSLog(@"文字改变");
//}

-(void)dealloc //移除通知
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}


@end

